<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据采集时间" prop="timestamp">
        <el-date-picker clearable
          v-model="queryParams.timestamp"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择数据采集时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实时电流" prop="currentAmperage">
        <el-input
          v-model="queryParams.currentAmperage"
          placeholder="请输入实时电流"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主轴温度" prop="spindleTemperature">
        <el-input
          v-model="queryParams.spindleTemperature"
          placeholder="请输入主轴温度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="振动幅度" prop="vibrationAmplitude">
        <el-input
          v-model="queryParams.vibrationAmplitude"
          placeholder="请输入振动幅度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="润滑油位百分比" prop="lubricantLevel">
        <el-input
          v-model="queryParams.lubricantLevel"
          placeholder="请输入润滑油位百分比"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前报警代码" prop="errorCode">
        <el-input
          v-model="queryParams.errorCode"
          placeholder="请输入当前报警代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前报警信息" prop="errorMessage">
        <el-input
          v-model="queryParams.errorMessage"
          placeholder="请输入当前报警信息"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前周期计数" prop="currentProductCount">
        <el-input
          v-model="queryParams.currentProductCount"
          placeholder="请输入当前周期计数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="OEE-可用性" prop="oeeAvailability">
        <el-input
          v-model="queryParams.oeeAvailability"
          placeholder="请输入OEE-可用性"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="OEE-性能" prop="oeePerformance">
        <el-input
          v-model="queryParams.oeePerformance"
          placeholder="请输入OEE-性能"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="OEE-质量" prop="oeeQuality">
        <el-input
          v-model="queryParams.oeeQuality"
          placeholder="请输入OEE-质量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总OEE" prop="oeeTotal">
        <el-input
          v-model="queryParams.oeeTotal"
          placeholder="请输入总OEE"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['equipment-service:realtimeData:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment-service:realtimeData:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment-service:realtimeData:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['equipment-service:realtimeData:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="realtimeDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据ID" align="center" prop="dataId" />
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="数据采集时间" align="center" prop="timestamp" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.timestamp, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前运行状态" align="center" prop="runStatus" />
      <el-table-column label="实时电流" align="center" prop="currentAmperage" />
      <el-table-column label="主轴温度" align="center" prop="spindleTemperature" />
      <el-table-column label="振动幅度" align="center" prop="vibrationAmplitude" />
      <el-table-column label="润滑油位百分比" align="center" prop="lubricantLevel" />
      <el-table-column label="当前报警代码" align="center" prop="errorCode" />
      <el-table-column label="当前报警信息" align="center" prop="errorMessage" />
      <el-table-column label="当前周期计数" align="center" prop="currentProductCount" />
      <el-table-column label="OEE-可用性" align="center" prop="oeeAvailability" />
      <el-table-column label="OEE-性能" align="center" prop="oeePerformance" />
      <el-table-column label="OEE-质量" align="center" prop="oeeQuality" />
      <el-table-column label="总OEE" align="center" prop="oeeTotal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['equipment-service:realtimeData:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['equipment-service:realtimeData:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备实时运行状态与关键指标对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="realtimeDataRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="数据采集时间" prop="timestamp">
          <el-date-picker clearable
            v-model="form.timestamp"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择数据采集时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实时电流" prop="currentAmperage">
          <el-input v-model="form.currentAmperage" placeholder="请输入实时电流" />
        </el-form-item>
        <el-form-item label="主轴温度" prop="spindleTemperature">
          <el-input v-model="form.spindleTemperature" placeholder="请输入主轴温度" />
        </el-form-item>
        <el-form-item label="振动幅度" prop="vibrationAmplitude">
          <el-input v-model="form.vibrationAmplitude" placeholder="请输入振动幅度" />
        </el-form-item>
        <el-form-item label="润滑油位百分比" prop="lubricantLevel">
          <el-input v-model="form.lubricantLevel" placeholder="请输入润滑油位百分比" />
        </el-form-item>
        <el-form-item label="当前报警代码" prop="errorCode">
          <el-input v-model="form.errorCode" placeholder="请输入当前报警代码" />
        </el-form-item>
        <el-form-item label="当前报警信息" prop="errorMessage">
          <el-input v-model="form.errorMessage" placeholder="请输入当前报警信息" />
        </el-form-item>
        <el-form-item label="当前周期计数" prop="currentProductCount">
          <el-input v-model="form.currentProductCount" placeholder="请输入当前周期计数" />
        </el-form-item>
        <el-form-item label="OEE-可用性" prop="oeeAvailability">
          <el-input v-model="form.oeeAvailability" placeholder="请输入OEE-可用性" />
        </el-form-item>
        <el-form-item label="OEE-性能" prop="oeePerformance">
          <el-input v-model="form.oeePerformance" placeholder="请输入OEE-性能" />
        </el-form-item>
        <el-form-item label="OEE-质量" prop="oeeQuality">
          <el-input v-model="form.oeeQuality" placeholder="请输入OEE-质量" />
        </el-form-item>
        <el-form-item label="总OEE" prop="oeeTotal">
          <el-input v-model="form.oeeTotal" placeholder="请输入总OEE" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RealtimeData">
import { listRealtimeData, getRealtimeData, delRealtimeData, addRealtimeData, updateRealtimeData } from "@/api/equipment-service/realtimeData"

const { proxy } = getCurrentInstance()

const realtimeDataList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipmentId: null,
    timestamp: null,
    runStatus: null,
    currentAmperage: null,
    spindleTemperature: null,
    vibrationAmplitude: null,
    lubricantLevel: null,
    errorCode: null,
    errorMessage: null,
    currentProductCount: null,
    oeeAvailability: null,
    oeePerformance: null,
    oeeQuality: null,
    oeeTotal: null
  },
  rules: {
    equipmentId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    timestamp: [
      { required: true, message: "数据采集时间不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询设备实时运行状态与关键指标列表 */
function getList() {
  loading.value = true
  listRealtimeData(queryParams.value).then(response => {
    realtimeDataList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    dataId: null,
    equipmentId: null,
    timestamp: null,
    runStatus: null,
    currentAmperage: null,
    spindleTemperature: null,
    vibrationAmplitude: null,
    lubricantLevel: null,
    errorCode: null,
    errorMessage: null,
    currentProductCount: null,
    oeeAvailability: null,
    oeePerformance: null,
    oeeQuality: null,
    oeeTotal: null
  }
  proxy.resetForm("realtimeDataRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dataId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加设备实时运行状态与关键指标"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _dataId = row.dataId || ids.value
  getRealtimeData(_dataId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备实时运行状态与关键指标"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["realtimeDataRef"].validate(valid => {
    if (valid) {
      if (form.value.dataId != null) {
        updateRealtimeData(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRealtimeData(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _dataIds = row.dataId || ids.value
  proxy.$modal.confirm('是否确认删除设备实时运行状态与关键指标编号为"' + _dataIds + '"的数据项？').then(function() {
    return delRealtimeData(_dataIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('equipment-service/realtimeData/export', {
    ...queryParams.value
  }, `realtimeData_${new Date().getTime()}.xlsx`)
}

getList()
</script>
